/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.mzk.oai.metadataRepository.analysis.marcxml;

import cz.mzk.oai.metadataRepository.analysis.ElementStatistics;
import cz.mzk.oai.metadataRepository.metadata.MultipleDataField;

/**
 *
 * @author Martin Rehanek, Martin.Rehanek@gmail.com
 */
public class MarcDataFieldStatistics extends ElementStatistics{

    //private int elementPresent = 0;
    //private int elementSingle = 0;
    //private int elementMultiple = 0;
    //private int elementMissing = 0;
    //private Map<String, Integer> valueOccurance;//of content of the element
    private char subfield;
    //private String name;

    public MarcDataFieldStatistics(String datafieldName, char subfield) {
        super();
        this.subfield = subfield;
        this.name = datafieldName;
    }

    public void addNewDatafield(MultipleDataField datafield, String recordId) {
        if (datafield.size() != 0) {
            elementPresent++;
            if (datafield.size() == 1) {
                elementSingle++;
                //predpokladame alespon jeden vyskyt subfieldu a vybirame jen prvni hodnotu
                String value = datafield.getSubfieldContents(subfield).get(0);
                Integer numberOfOccurence = valueOccurance.get(value);
                if (numberOfOccurence == null) {
                    valueOccurance.put(value, Integer.valueOf(1));
                } else {
                    Integer newNumberOfOccurence = Integer.valueOf(numberOfOccurence+1);
                    //System.out.println("HERE");
                    valueOccurance.put(value, newNumberOfOccurence);
                }
            } else {
                elementMultiple++;
            }
        } else {
            elementMissing++;
            System.out.println("missing, id: " + recordId);
        }
    }

    public void printResults() {
        System.out.println("datafield : " + name);
        System.out.println("records with datafield present: " + elementPresent);
        System.out.println("records with datafield missing: " + elementMissing);
        System.out.println("records with multiple datafield elements: " + elementMultiple);
        System.out.println("records with single datafield element: " + elementSingle);
        System.out.println("values in subfield '" + subfield + "' (only for single element):");
        for (String value : valueOccurance.keySet()) {
            System.out.println(value + ":" + valueOccurance.get(value));
        }
    }
}
